package com.customer.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.customer.mall.domain.Product;
import com.customer.mall.domain.User;
import com.customer.mall.domain.vo.CartVO;
import com.customer.mall.domain.vo.UserVO;
import com.customer.mall.mapper.CartMapper;
import com.customer.mall.domain.Cart;
import com.customer.mall.service.ICartService;
import com.customer.mall.service.IProductService;
import com.customer.mall.service.IUserService;
import com.customer.mall.util.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author BaiGGG
 * @since 2023-06-25
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Autowired
    private IProductService productService;

    @Autowired
    private IUserService userService;

    @Override
    public ResponseResult<Page<CartVO>> pageCart(String phone, Integer pageNum, Integer pageSize) {
        Page<Cart> cartPage = new Page<>(pageNum, pageSize);
        Integer userId = null;
        System.out.println(phone);
        if(StringUtils.hasText(phone)){
            User getUserPhone = userService.getOne(new QueryWrapper<User>().select("id").eq("phone", phone));
            userId = getUserPhone.getId();
        }
        Page<CartVO> cartVOPage = new Page<>();
        cartVOPage.setRecords(page(cartPage, new QueryWrapper<Cart>().eq(!Objects.isNull(userId), "user_id", userId))
                .getRecords().stream().map(item -> {
                    CartVO cartVO = new CartVO();
                    BeanUtils.copyProperties(item, cartVO);
                    Product getProduct = productService.getById(item.getProdId());
                    cartVO.setProdName(getProduct.getTitle());
                    cartVO.setPrice(getProduct.getPrice());
                    cartVO.setImg(getProduct.getImg());
                    cartVO.setUserName(userService.getUserById(Long.valueOf(item.getUserId())).getNickName());
                    return cartVO;
                }).collect(Collectors.toList()));
        BeanUtils.copyProperties(cartPage, cartVOPage, "records");
        return ResponseResult.okResult(cartVOPage);
    }

    @Override
    public ResponseResult<CartVO> getCartById(Integer id) {
        Cart getCart = getById(id);
        CartVO resCartVO = new CartVO();
        BeanUtils.copyProperties(getCart, resCartVO);
        Product getProduct = productService.getById(resCartVO.getProdId());
        resCartVO.setProdName(getProduct.getTitle());
        resCartVO.setPrice(getProduct.getPrice());
        resCartVO.setImg(getProduct.getImg());
        resCartVO.setUserName(userService.getUserById(Long.valueOf(resCartVO.getUserId())).getNickName());
        return ResponseResult.okResult(resCartVO);
    }

    @Override
    public ResponseResult<String> insertOrUpdateCart(Cart cart) {
        if (Objects.isNull(cart.getProdId()) || Objects.isNull(cart.getUserId())) {
            return ResponseResult.failResult("商品id或用户id不能为空");
        }
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getProdId, cart.getProdId())
                .eq(Cart::getUserId, cart.getUserId());
        Cart getCart = getOne(cartLambdaQueryWrapper);

        // 不存在则插入
        if(Objects.isNull(getCart)){
            if(!save(cart)){
                return ResponseResult.failResult("保存失败");
            }
            return ResponseResult.okResult("更新一条购物车记录");
        }
        // 存在更新
        getCart.setNum(getCart.getNum() + cart.getNum());
        if (!updateById(getCart)) {
            return ResponseResult.failResult("更新失败");
        }
        return ResponseResult.okResult("更新一条购物车记录");
    }

    @Override
    public ResponseResult<String> updateCart(Cart cart) {
        if (Objects.isNull(cart.getId())) {
            return ResponseResult.failResult("购物车id不能为空");
        }
        if (!updateById(cart)) {
            return ResponseResult.failResult("修改失败");
        }
        return ResponseResult.okResult();
    }

}
